<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <div class="box">测试指针事件</div>

  <script>
    const body = document.querySelector('body')
    const box = document.querySelector('.box')

    // 1. 指针按下事件
    // box.onpointerdown = e => {
    //   console.log('123')
    // }

    // 2. 指针移动事件:
    // box.onpointermove = e => {
    //   // console.log('指针移动')
    // }

    // 3.
    body.onpointermove = e => {
      // console.log(e.clientX)

      // 输出的就是当前元素实际的宽度:
      // console.log(box.getBoundingClientRect().width)

      // 输出:当前指针, 以当前 box 元素中央为0初始位置, 左负右正:
      const targetX = e.clientX - box.getBoundingClientRect().width / 2
      console.log(targetX)
    }

    
  </script>
</body>
</html>